﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Admx_Generator
{
    internal partial class CategoryViewer : UserControl
    {
        private Category _displayedCategory = null;
        private AdmlFile _admlFile = null;

        internal CategoryViewer(Category categoryToDiplay, AdmlFile adml)
        {
            InitializeComponent();
            this._admlFile = adml;
            this.fltTxtBxName.FilterMethod = EasyCompany.Controls.FilteredTextbox.FilterMethods.AllowedCharacters;
            List<char> allowedChar = Utilities.GetAllowedCharacters();
            this.fltTxtBxName.AllowedCharacters.AddRange(allowedChar.ToArray());
            this.CategoryToDisplay = categoryToDiplay;
        }

        #region (properties)

        /// <summary>
        /// The <see cref="Category"/> currently displayed.
        /// </summary>
        internal Category CategoryToDisplay
        {
            get { return this._displayedCategory; }

            set
            {
                this._displayedCategory = value;
                this.txtBxDisplayName.ReadOnly = (this._displayedCategory.ParentCategory !=null && !String.IsNullOrEmpty(this._displayedCategory.ParentCategory.NamespacePrefix));
                this.txtBxExplain.ReadOnly = (this._displayedCategory.ParentCategory != null && !String.IsNullOrEmpty(this._displayedCategory.ParentCategory.NamespacePrefix));
                this.UpdateDisplay();
            }
        }

        /// <summary>
        /// The Culture used to get localized string for the DisplayName and the ExplainText.
        /// </summary>
        internal AdmlFile CurrentLanguage 
        {
            get { return this._admlFile; }
            set 
            { 
                this._admlFile = value;
                this.UpdateDisplay();
            }
        }

        #endregion (properties)

        #region (methods)

        private void UpdateDisplay()
        {
            this.fltTxtBxName.Text = this.CategoryToDisplay.Name;
            this.txtBxDisplayName.Text = this.CurrentLanguage.GetString(this.CategoryToDisplay.DisplayName);
            this.txtBxCulture.Text = this.CurrentLanguage.ToString();
            if (this.CategoryToDisplay.ParentCategory != null)
            {
                this.txtBxParentCategory.Text = this.CategoryToDisplay.ParentCategory.ToString();
            }
            this.txtBxExplain.Text = this.CurrentLanguage.GetString(this.CategoryToDisplay.ExplainText);
        }

        #endregion (private methods)

        #region (UI Management)

        private void txtBxExplain_TextChanged(object sender, EventArgs e)
        {
            this.CurrentLanguage.ModifyString(this._displayedCategory.ExplainText, this.txtBxExplain.Text);            
        }

        private void txtBxDisplayName_TextChanged(object sender, EventArgs e)
        {
            this.CurrentLanguage.ModifyString(this._displayedCategory.DisplayName, this.txtBxDisplayName.Text);
        }

        #endregion (UI Management)
    }
}
